//
//  CmtActDetailViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtActDetailViewController.h"
#define  kUrlString @"http://api.izhangchu.com/"
#import "RequestManager.h"

#import "CmtSecAttenRelates.h"
#import "CmtActModel.h"

#import "CmtActDetailCollectionViewCell.h"
#import "CmtSecMJoinViewController.h"
#import "CmtCollectionReusableView.h"
#import "CmtActDetailButCollectionViewCell.h"
typedef NS_ENUM(NSInteger,ButtonData){

    CreateBut,
    CommentBut,
    AgreeBut

};

@interface CmtActDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)CmtActModel *dataModel;

@property(nonatomic,strong)NSMutableArray *createArr;
@property(nonatomic,strong)NSMutableArray *commentArr;
@property(nonatomic,strong)NSMutableArray *agreeArr;

@property(nonatomic,assign)ButtonData butData;
@property(nonatomic,strong)UIView *lineV;

@property(nonatomic,assign)BOOL isClink;
@property(nonatomic,strong)CmtCollectionReusableView *headView;

@property(nonatomic,strong)UIView *topView;
@end

@implementation CmtActDetailViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.topView.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    self.topView.hidden = YES;
}



//自定义导航栏
-(void)setNavView{
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _topView.backgroundColor = [UIColor whiteColor];
    button.frame = CGRectMake(10, 30, 50, 30);
    [button setTitle:@"<—" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:button];
    [self.navigationController.view addSubview:_topView];
    
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    shareButton.frame = CGRectMake(kWidth - 70, 30, 50, 30);
    [shareButton setImage:[UIImage imageNamed:@"share.png"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:shareButton];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((kWidth - 100)/2, 30, 100, 30)];
    label.text = [NSString stringWithFormat:@"%@",self.model.title];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    [self.topView addSubview:label];
    
    self.view.backgroundColor = [UIColor yellowColor];
}

-(void)buton:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setNavView];

    self.navigationController.navigationBar.translucent = NO;

    //   设置collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    [self.view addSubview:_collectionView];

//    注册表头
//    [self.collectionView registerNib:[UINib nibWithNibName:@"CmtCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CmtHeader"];
    [self.collectionView registerClass:[CmtCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CmtHeader"];
    [self.collectionView registerClass:[CmtActDetailCollectionViewCell class] forCellWithReuseIdentifier:@"CmtItems"];
    
    [self.collectionView registerClass:[CmtActDetailButCollectionViewCell class] forCellWithReuseIdentifier:@"CmtItem"];

//    初始化数组
    self.commentArr = [[NSMutableArray alloc]initWithCapacity:10];
    self.agreeArr = [[NSMutableArray alloc]initWithCapacity:10];
    self.createArr = [[NSMutableArray alloc]initWithCapacity:10];
    
//    网络请求
    [self requestHeadViewData];
    [self requestCreateData];
    [self requestCommentData];
    [self requestAgreeData];
    
    self.butData = CreateBut;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.butData == CreateBut) {
        return self.createArr.count + 1;
    }
    if (self.butData == AgreeBut) {
        return self.agreeArr.count + 1;
    }
    return self.commentArr.count + 1;
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
//返回Item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        CmtActDetailButCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CmtItem" forIndexPath:indexPath];
        [cell.but1 addTarget:self action:@selector(But1Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.but2 addTarget:self action:@selector(but2Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.but3 addTarget:self action:@selector(but3Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
    else{
        CmtActDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CmtItems" forIndexPath:indexPath];
        
        if (self.butData == CreateBut) {
            cell.model = _createArr[indexPath.row - 1];
        }
        else if (self.butData == AgreeBut) {
            cell.model = _agreeArr[indexPath.row - 1];
        }
        else{
            cell.model = _commentArr[indexPath.row  - 1];
        }
        
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return CGSizeMake(kWidth, 50);
    }
    return CGSizeMake((kWidth - 5) / 2, (kWidth - 5) / 2 + 70);
}

-(void)But1Action:(UIButton *)button{
    
//    NSLog(@"%@",[button.superview.superview class]);
    NSLog(@"fklfdklfdkldfslk;d");
    CmtActDetailButCollectionViewCell *cell = (CmtActDetailButCollectionViewCell*)button.superview.superview;
    cell.lineV.frame = CGRectMake(50 + (kWidth - 100) / 3 * 0 , 47, (kWidth - 100) / 3, 3);
    self.butData = CreateBut;
    [self requestCreateData];
    
}
-(void)but2Action:(UIButton *)button{
    
//    NSLog(@"%@",[button.superview.superview class]);
    
    CmtActDetailButCollectionViewCell *cell = (CmtActDetailButCollectionViewCell*)button.superview.superview;
    cell.lineV.frame= CGRectMake(50 + (kWidth - 100) / 3 * 1 , 47, (kWidth - 100) / 3, 3);
    self.butData = AgreeBut;
    [self requestAgreeData];
}
-(void)but3Action:(UIButton *)button{
    
//    NSLog(@"%@",[button.superview.superview class]);
    
    CmtActDetailButCollectionViewCell *cell = (CmtActDetailButCollectionViewCell*)button.superview.superview;
    
    cell.lineV.frame = CGRectMake(50 + (kWidth - 100) / 3 * 2 , 47, (kWidth - 100) / 3, 3);
    self.butData = CommentBut;
    [self requestCommentData];
    
}


//返回区头的方法
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  
        CmtCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CmtHeader" forIndexPath:indexPath];
        if (_dataModel.modelArray.count != 0) {
            
            headView.model = _dataModel;
            headView.block = ^(){
                
                NSLog(@"%ld",_dataModel.modelArray.count);
                
                CmtSecMJoinViewController *CmtSecMjVc = [[CmtSecMJoinViewController alloc]init];
                CmtSecMjVc.activity_id = self.activity_id;
                [self.navigationController pushViewController:CmtSecMjVc animated:YES];
            };
        }
        
        
        self.headView = headView;
        return headView;
    }
    return nil;
}

//返回表头高度的方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CmtCollectionReusableView *CmtReusable = [[CmtCollectionReusableView alloc]init];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadheightHead:) name:@"loadHeightofHead" object:nil];
    
    if (_dataModel.modelArray == nil) {
        return CGSizeMake(kWidth, 100 + kWidth / 2 + 50 + 80);
    }
    
    return [CmtReusable headerViewHeight:_model];
}

-(void)loadheightHead:(NSNotification *)notification{


    
    NSArray *array = notification.object;
    NSNumber *boolNum = (NSNumber *)array[0];
    self.isClink = boolNum.boolValue;
    CmtCollectionReusableView *cell = (CmtCollectionReusableView *)array.lastObject;
    
    
    if (self.isClink) {
        
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
        CGRect bounds = [cell.content.text boundingRectWithSize:CGSizeMake(kWidth - 40, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
        self.headView.frame = CGRectMake(0,0,kWidth, bounds.size.height + kWidth / 2 + 50 + 80);
        [self.collectionView reloadData];
    }else{
    
        self.headView.frame = CGRectMake(0, 0,kWidth, 100 + kWidth / 2 + 50 + 80);
        [self.collectionView reloadData];
    }

}



//表头数据请求
-(void)requestHeadViewData{

    NSDictionary *pardic = @{@"methodName":@"ShequActivityview",@"version":@"4.3",@"id":self.activity_id};
    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:pardic finish:^(NSData *data) {
        NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = jsDic[@"data"];
        
//        NSLog(@"%@",jsDic);
        self.dataModel = [[CmtActModel alloc]init];
        [_dataModel setValuesForKeysWithDictionary:dataDic];
        
        NSArray *activity_usersArr = jsDic[@"data"][@"active_users"];
        for (NSDictionary *dic in activity_usersArr) {
            CmtSecAttenRelates *relates = [[CmtSecAttenRelates alloc]init];
            [relates setValuesForKeysWithDictionary:dic];
            
            [_dataModel.modelArray addObject:relates];
            
        }
        [self.collectionView reloadData];
        
    } error:nil];
}

//最新作品数据请求
-(void)requestCreateData{

    [self.createArr removeAllObjects];
    NSDictionary *pardic = @{@"methodName":@"ShequActivitypost",@"version":@"4.3",@"activity_id":self.activity_id,@"order_by":@"create_time",@"page":@"1",@"size":@"10"};
    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:pardic finish:^(NSData *data) {
        NSDictionary *jsdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = jsdic[@"data"];
        NSDictionary *dataArr = dataDic[@"data"];
        for (NSDictionary *dic in dataArr) {
            CmtActModel *model = [[CmtActModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [model setValuesForKeysWithDictionary:dic];
            [self.createArr addObject:model];
        }
        [self.collectionView reloadData];
        
    } error:nil];
}
//请求评论列表
-(void)requestCommentData{
    [self.commentArr removeAllObjects];
    NSDictionary *pardic = @{@"methodName":@"ShequActivitypost",@"version":@"4.3",@"activity_id":self.activity_id,@"order_by":@"comment_count",@"page":@"1",@"size":@"10"};
    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:pardic finish:^(NSData *data) {
        NSDictionary *jsdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = jsdic[@"data"];
        NSDictionary *dataArr = dataDic[@"data"];
        for (NSDictionary *dic in dataArr) {
            CmtActModel *model = [[CmtActModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [model setValuesForKeysWithDictionary:dic];
            [self.commentArr addObject:model];
            NSLog(@"%ld",_commentArr.count);
        }
        [self.collectionView reloadData];
        
    } error:nil];
}


-(void)requestAgreeData{
    [self.agreeArr removeAllObjects];
    NSDictionary *pardic = @{@"methodName":@"ShequActivitypost",@"version":@"4.3",@"activity_id":self.activity_id,@"order_by":@"agree_count",@"page":@"1",@"size":@"10"};
    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:pardic finish:^(NSData *data) {
        NSDictionary *jsdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = jsdic[@"data"];
        NSDictionary *dataArr = dataDic[@"data"];
        for (NSDictionary *dic in dataArr) {
            CmtActModel *model = [[CmtActModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [model setValuesForKeysWithDictionary:dic];
            [self.agreeArr addObject:model];
        }
        [self.collectionView reloadData];
        
    } error:nil];
}


//分享
-(void)shareAction:(UIBarButtonItem *)barButton{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
